package com.caigou.modules.login.repository

import com.caigou.comm.base.bean.HttpResult
import com.caigou.comm.base.bean.UserBean
import com.caigou.modules.login.network.HttpLoginService
import dagger.hilt.android.scopes.ActivityRetainedScoped
import kotlinx.coroutines.flow.Flow
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/09/27
 */
@ActivityRetainedScoped
class LoginRepository @Inject constructor() {

    fun register(nickName: String, mobile: String, password: String, smsCode: String, invitationCode: String): Flow<HttpResult<UserBean>> {
        val jsonObject = JSONObject()
        jsonObject.put("nickName", nickName)
        jsonObject.put("password", password)
        jsonObject.put("mobile", mobile)
        jsonObject.put("smsCode", smsCode)
        jsonObject.put("otherInvitationCode", invitationCode)
        val body = jsonObject.toString().toRequestBody()
        return HttpLoginService.service.register(body)
    }

    fun launcher(mobile: String, password: String): Flow<HttpResult<UserBean>> {
        val jsonObjects = JSONObject()
        jsonObjects.put("mobile", mobile)
        jsonObjects.put("password", password)
        val body = jsonObjects.toString().toRequestBody()
        return HttpLoginService.service.launcher(body)
    }

    fun getSmsCode(mobile: String): Flow<HttpResult<String>> {
        return HttpLoginService.service.getSmsCode(mobile)
    }

    fun forgetPwd(mobile: String, smsCode: Long, newPwd: String): Flow<HttpResult<String>> {
        val jsonObject = JSONObject()
        jsonObject.put("mobile", mobile)
        jsonObject.put("smsCode", smsCode)
        jsonObject.put("newPwd", newPwd)
        val body = jsonObject.toString().toRequestBody()
        return HttpLoginService.service.forgetPwd(body)
    }
}